// instanceof运算符用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上。

const myInstanceof = (left, right) => {
  // 基本数据类型都返回false
  if (typeof left !== "object" || left === null) return false;
  let proto = Object.getPrototypeOf(left);
  while (true) {
    if (proto === null) return false;
    if (proto === right.prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
};

// 递归方式
const myInstanceof1 = (left, right) => {
  // 基本数据类型都返回false
  if (typeof left !== "object" || left === null) return false;
  let proto = Object.getPrototypeOf(left);
  if (proto === null) return false;
  if (proto === right.prototype) return true;
  return myInstanceof1(proto, right)
};

// 遍历方式2
const myInstanceof2 = (left, right) => {
  // 基本数据类型都返回false
  if (typeof left !== "object" || left === null) return false;
  let proto = left;
  while (proto = Object.getPrototypeOf(proto)) {
    if (proto === right.prototype) return true;
  }
  return false
};

// 测试
let Fn = function () { }
let p1 = new Fn()
console.log(myInstanceof2({}, Object));
console.log(myInstanceof2(p1, Fn));